﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace LinqDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewData();
            }
        }

        // 添加
        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (SCYHDataContext scyh = new SCYHDataContext())
            {
                t_users user = new t_users
                {
                    userName = txtCountryName.Text.Trim(),
                    password = txtCountryCode.Text.Trim(),
                    birthday = DateTime.Now,
                    regDate = DateTime.Now,
                    advPassword = "8888"
                };

                scyh.Connection.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["SCYH"];
                scyh.t_users.InsertOnSubmit(user);
                scyh.SubmitChanges();

                ViewData();            
            }
        }

        // 更新
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SCYHDataContext db = new SCYHDataContext();
            db.Connection.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["SCYH"];

            var user = db.t_users.Single(p => p.Id == Convert.ToInt32(lblID.Text));
            user.userName = txtCountryName.Text;
            user.password = txtCountryCode.Text;
            db.SubmitChanges();

            ViewData();
        }

        // 加载数据
        public void ViewData()
        {     
            // 日志
            StreamWriter sw = new StreamWriter(Server.MapPath(DateTime.Now.ToString("yyyy-MM-dd") + "_log.txt"), true);

            SCYHDataContext db = new SCYHDataContext();
            db.Connection.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["SCYH"];

            db.Log = sw;

            var user =
                from c in db.t_users
                select c;
        
            /********* 转换json格式的类库 ****************/
            List<t_users> userList = new List<t_users>();
            t_users tem1 = new t_users();
            tem1.Id = 1;
            tem1.userName = "001";
            tem1.advPassword = "001";
            userList.Add(tem1);

            //string aa = LitJson.JsonMapper.ToJson(userList);
            //Response.Write(aa);
            /*********************************************/

            GridView1.DataSource = user;
            GridView1.DataBind();
        }

        // 行梆定事件
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdEdit")
            {
                lblID.Text = e.CommandArgument.ToString();

                SCYHDataContext db = new SCYHDataContext();
                db.Connection.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["SCYH"];

                var user =
                from c in db.t_users
                where(c.Id == Convert.ToInt32(e.CommandArgument.ToString()))
                select c;

                foreach (t_users temp in user)
                {
                    txtCountryName.Text = temp.userName;
                    txtCountryCode.Text = temp.password;
                }

                btnSave.Visible = false;

                ViewData();
            }
            else if (e.CommandName == "cmdDelete")
            {
                SCYHDataContext db = new SCYHDataContext();
                db.Connection.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["SCYH"];

                var user =
                from c in db.t_users
                where(c.Id == Convert.ToInt32(e.CommandArgument.ToString()))
                select c;

                db.t_users.DeleteAllOnSubmit(user);
                db.SubmitChanges();

                ViewData();
            }
        }

    }
}
